//
//  XJLoginView.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/8.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJLoginView.h"

@interface XJLoginView ()<UITextFieldDelegate>

@property (nonatomic ,strong) UIButton * forgetPasswdBtn;

@property (nonatomic ,strong) UILabel * tipsLab;

@property (nonatomic ,copy) NSArray * otherLoginArray;

@end

@implementation XJLoginView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = WHITE_COLOR;
        [self setupSubviews];
    }
    return self;
}

- (ZYTextField *)loginNameTextField{
    if (!_loginNameTextField) {
        ZYTextField * loginNameTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(22), ADAPTHeight(30), KWidth - ADAPTWidth(22)*2, ADAPTHeight(50)) placeholder:@"登录手机/昵称" keyboardType:UIKeyboardTypeDefault leftViewImageName:@"login_name" textEntry:NO];
        self.loginNameTextField = loginNameTextField;
        [self addSubview:loginNameTextField];
    }
    return _loginNameTextField;
}

- ( ZYTextField *)loginCodeTextField{
    
    if (!_loginCodeTextField) {
        ZYTextField * loginCodeTextField = [[ZYTextField alloc]initWithFrame:CGRectMake(ADAPTWidth(22), CGRectGetMaxY(self.loginNameTextField.frame)+0.5, KWidth - ADAPTWidth(22)*2, ADAPTHeight(50)) placeholder:@"您的登录密码" keyboardType:UIKeyboardTypeDefault leftViewImageName:@"login_password" textEntry:YES];
        self.loginCodeTextField = loginCodeTextField;
        [self addSubview:loginCodeTextField];
    }
    return _loginCodeTextField;
}

- (UIButton *)forgetPasswdBtn{
    
    if (!_forgetPasswdBtn) {
        UIButton * forgetPasswdBtn = [UIButton new];
        self.forgetPasswdBtn = forgetPasswdBtn;
       // [self addSubview:forgetPasswdBtn];
    }
    return _forgetPasswdBtn;
}


- (UIButton *)loginButton{
    if (!_loginButton) {
        UIButton * loginButton = [UIButton new];
        self.loginButton = loginButton;
        [self addSubview:loginButton];
    }
    return _loginButton;
    
}

- (UIButton *)registButton{
    
    if (!_registButton) {
        UIButton * registButton = [UIButton new];
        self.registButton = registButton;
        [self addSubview:registButton];
    }
    return _registButton;
}

- (UILabel *)tipsLab{
    
    if (!_tipsLab) {
        UILabel * tipsLab = [UILabel new];
        self.tipsLab = tipsLab;
        [self addSubview:tipsLab];
    }
    return _tipsLab;
    
}

- (NSArray *)otherLoginArray{
    
    if (!_otherLoginArray) {
        _otherLoginArray = @[@[@"share_wechat",@"微信"],
                             @[@"share_ weibo",@"微博"],
                             @[@"share_qq",@"QQ"]
                             ];
    }
    return _otherLoginArray;
}

 //设置子控件
- (void)setupSubviews{
    //登录名
    self.loginNameTextField.delegate = self;
   // [self.loginNameTextField addTarget:self actio n:@selector(loginNameTextFieldChanged:) forControlEvents:UIControlEventEditingChanged];

    //登录密码
    self.loginCodeTextField.secureTextEntry=YES;
    self.loginCodeTextField.delegate = self;

    //忘记密码
    self.forgetPasswdBtn.titleLabel.font = XJFONTSET_R(12);
    [self.forgetPasswdBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.forgetPasswdBtn setTitleColor:XJCOLOR_RGB(0, 96, 155) forState:UIControlStateNormal];
    CGSize forgetPasswdBtnSize = [UIButton getSizeFromButtonTitle:@"忘记密码" Font:XJFONTSET_R(12)];
    self.forgetPasswdBtn.frame = CGRectMake(0, 0, forgetPasswdBtnSize.width, forgetPasswdBtnSize.height);
    self.loginCodeTextField.rightView = self.forgetPasswdBtn;
    self.loginCodeTextField.rightViewMode = UITextFieldViewModeAlways;
    [self.forgetPasswdBtn addTarget:self action:@selector(forgetPasswdBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //登录
    self.loginButton.frame =CGRectMake(ADAPTWidth(25), CGRectGetMaxY(self.loginCodeTextField.frame)+ADAPTHeight(40), KWidth - ADAPTWidth(25)*2, ADAPTHeight(40));
    self.loginButton.backgroundColor = MAIN_COLOR;
    [self.loginButton setTitleColor:XJCOLOR_RGB(17, 17, 17) forState:UIControlStateNormal];
    self.loginButton.titleLabel.font = XJFONTSET_R(15);
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    self.loginButton.layer.masksToBounds = YES;
    self.loginButton.layer.cornerRadius = self.loginButton.frame.size.height/2;
    [self.loginButton addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    //注册
    self.registButton.frame =CGRectMake(ADAPTWidth(25), CGRectGetMaxY(self.loginButton.frame)+ADAPTHeight(20), KWidth - ADAPTWidth(25)*2, ADAPTHeight(40));
    self.registButton.backgroundColor = WHITE_COLOR;
    [self.registButton setTitleColor:XJCOLOR_RGB(17, 17, 17) forState:UIControlStateNormal];
    self.registButton.titleLabel.font = XJFONTSET_R(15);
    [self.registButton setTitle:@"注册新账号" forState:UIControlStateNormal];
    self.registButton.layer.masksToBounds = YES;
    self.registButton.layer.cornerRadius = self.registButton.frame.size.height/2;
    self.registButton.layer.borderWidth = 0.5;
    self.registButton.layer.borderColor = XJCOLOR_RGB(121, 130, 133).CGColor;
    [self.registButton addTarget:self action:@selector(registButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //第三方登录
    self.tipsLab.text = @"—— 其他方式登录 ——";
    self.tipsLab.font = XJFONTSET_R(12);
    self.tipsLab.textColor = XJCOLOR_RGB(157, 165, 168);
    CGFloat tipsLabW = [UILabel labelWithLabel:self.tipsLab getWidthWithTitle:self.tipsLab.text font:self.tipsLab.font];
    CGFloat tipsLabH = [UILabel labelWithLabel:self.tipsLab getHeightByWidth:tipsLabW title:self.tipsLab.text font:self.tipsLab.font];
    self.tipsLab.frame = CGRectMake((KWidth-tipsLabW)/2, CGRectGetMaxY(self.registButton.frame)+ADAPTHeight(189), tipsLabW, tipsLabH);
//    self.tipsLab.sd_layout
//    .bottomSpaceToView(self, ADAPTHeight(127))
//    .centerXEqualToView(self)
//    .widthIs(tipsLabW)
//    .heightIs(tipsLabH);
    
    [self.otherLoginArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIButton * otherLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        otherLoginBtn.frame = CGRectMake(ADAPTWidth(83)+ idx*(ADAPTWidth(32)+ADAPTWidth(57)),CGRectGetMaxY(self.tipsLab.frame)+ADAPTHeight(34), ADAPTWidth(40),ADAPTHeight(50));
        otherLoginBtn.tag = idx;

        [otherLoginBtn buttonWithTitle:[obj lastObject] titleFont:XJFONTSET_R(12) titleColor:XJCOLOR_RGB(17, 17, 17) imageType:ImageTypeImg imageName:[obj firstObject] withTarget:self action:@selector(otherLoginBtnClick:) style:ImgTextBtnStyleTop margin:10];
        [self addSubview:otherLoginBtn];

//        XJLog(@"otherLoginBtn  %f",otherLoginBtn.frame.origin.y);
//        XJLog(@"yyy == %f",CGRectGetMaxY(self.tipsLab.frame));
//        XJLog(@"yyy == %f",self.tipsLab.frame.origin.y);

    }];

}

#pragma mark 忘记密码
- (void)forgetPasswdBtnClick:(UIButton *)sender{
    if (_delegate && [_delegate respondsToSelector:@selector(forgetUserPasswordAction)]) {
        
        [_delegate forgetUserPasswordAction];
    }
}

#pragma mark 登录
- (void)loginButtonClick:(UIButton *)sender{
    
    if (_delegate && [_delegate respondsToSelector:@selector(loginWithUserName:code:)]) {
        [_delegate loginWithUserName:self.loginNameTextField.text code:self.loginCodeTextField.text];
    }
}

#pragma mark 注册
- (void)registButtonClick:(UIButton *)sender{
    
    [JRToast showWithText:@"点击注册按钮"];
    
    if (_delegate && [_delegate respondsToSelector:@selector(registAction)]) {
        [_delegate registAction];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.loginNameTextField endEditing:YES];
    [self.loginCodeTextField endEditing:YES];

}

- (void)otherLoginBtnClick:(UIButton *)sender{
    
      XJLog(@"点击了-> %@",[[self.otherLoginArray objectAtIndex:sender.tag] lastObject]);
    
    if (sender.tag == 0) { //微信登录
        if ([self.delegate respondsToSelector:@selector(loginByWeiXin)]) {
            [self.delegate loginByWeiXin];
        }
    }else if (sender.tag == 1){ // 微博登录
        if ([self.delegate respondsToSelector:@selector(loginByWeiBo)]) {
            [self.delegate loginByWeiBo];
        }
    }else if (sender.tag == 2){ //QQ登录
        if ([self.delegate respondsToSelector:@selector(loginByQQ)]) {
            [self.delegate loginByQQ];
        }
    }else {
        XJLog(@"无效");
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
